/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

/**
 *
 * @author Phung
 */
public class MySQLDataHelper_DAO {
    
    private static Connection connection;

    public static Connection getConnection() {
    return connection;
    }
    public static Connection Connect(){
        try {
            Driver drive = new org.gjt.mm.mysql.Driver();
            DriverManager.registerDriver(drive);
            String conn = "jdbc:mysql://localhost:3306/qlds_db";
            Properties info = new Properties();
            info.setProperty("characterEncoding", "utf8");
            info.setProperty("user", "root");
            info.setProperty("password", "123456");
            connection = DriverManager.getConnection(conn, info);
            return connection;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return null;
        }
    }

    public static void open() {
        try {
            Driver drive = new org.gjt.mm.mysql.Driver();
            DriverManager.registerDriver(drive);
            String conn = "jdbc:mysql://localhost:3306/qlds_db";
            Properties info = new Properties();
            info.setProperty("characterEncoding", "utf8");
            info.setProperty("user", "root");
            info.setProperty("password", "123456");
            connection = DriverManager.getConnection(conn, info);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    public static int executeUpdate(String sql) {
        int n = -1;
        try {
            Statement sm = connection.createStatement();
            n = sm.executeUpdate(sql);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return n;
    }

    public static void close() {
        try {
            connection.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    public static ResultSet executeQuery(String sql) {
        ResultSet rs = null;
        try {
            Statement sm = connection.createStatement();
            rs = sm.executeQuery(sql);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return rs;
    }
}
